<?php

class AreaPopularAction extends BaseAction{

	public function index() {
		$pagedata = model('AreaPopular')->kPageQuery($where,$order,20,$field);
		$this->assignRequestParams();
		$this->assign('pagedata', $pagedata);
		$this->display();
	}


	public function add() {
		$this -> display("form");
	}


	public function doAdd(){
		$map['title'] = t($_POST['title']);
		$map['item_sum'] = t($_POST['item_sum']);
		$map['title'] = stripslashes($map['title']);
		$gparea = explode(',',$_POST['gparea']);
		if(!empty($gparea[0])) {
			$map['province_id'] 	=	intval($gparea[0]);
			$map['city_id'] 	=	intval($gparea[1]);
			$map['area_id'] 	=	intval($gparea[2]);
		}
		if(empty($map['title']))$this->error('名称必须输入！');
		if(empty($map['province_id'])||empty($map['city_id'])||empty($map['area_id']))$this->error('请选择地区,选择到区一级！');
		if(!$this->checktitle($map['title'] ,$map['city_id'],null))$this->error('该城市，地点已存在！');
		
		$map['ctime'] 	=	time();
		$id = model('AreaPopular') ->add ( $map );
			
		if(!empty($id)){
			$this->assign('jumpUrl',U('/AreaPopular/index'));
			$this->success('新增成功！');
		}else{
			$this->error('新增失败！');
		}
	}

	public function edit() {
		$id = $_GET['id'];
		if(empty($id))$this->error('参数错误！');
			
		$popular = model('AreaPopular')->where('popu_id='.$_GET['id'])->find();
		if(empty($popular))$this->error('地点不存在！');
		
		$this->assign('type', 'edit');
		$this->assign('popular', $popular);
		$this->display('form');
	}

	public function doEdit(){
			
		$id = intval($_POST['popu_id']);
		if(empty($id))$this->error('参数错误！');
		$map['popu_id'] = $id;
		$map['title'] = t($_POST['title']);
		$map['item_sum'] = t($_POST['item_sum']);
		$map['title'] = stripslashes($map['title']);
		$gparea = explode(',',$_POST['gparea']);
		if(!empty($gparea[0])) {
			$map['province_id'] 	=	intval($gparea[0]);
			$map['city_id'] 	=	intval($gparea[1]);
			$map['area_id'] 	=	intval($gparea[2]);
		}
		if(empty($map['title']))$this->error('名称必须输入！');
		
		if(empty($map['province_id'])||empty($map['city_id'])||empty($map['area_id']))$this->error('请选择地区,选择到区一级！');
		
		if(!$this->checktitle($map['title'] ,$map['city_id'],$id))$this->error('该城市，地点已存在！');
		
		$map['item_sum'] = $sum;
		$map['mtime'] = time();
		model('AreaPopular')->save($map);
		
		/** 更新店铺总数 **/
		$this->_refreshItemSum($map['title']);
		
		//上传图标附件
		if(!empty($id)){
			$this->assign('jumpUrl',U('/AreaPopular/index'));
			$this->success('修改成功！');
		}else{
			$this->error('修改失败！');
		}

			
	}

	function checktitle($title,$city_id,$id=null){
		$where['title']=$title;
		$where['city_id']=$city_id;
		if(!empty($id))$where['popu_id']=array('NEQ',$id);
		$count = model('AreaPopular')->where($where)->count();
		//echo model('AreaPopular')->getLastSql()." - ".$count;
		return $count == 0;
	}
	
	public function del() {
		$_POST['ids']	= explode(',', t($_POST['ids']));
		if (empty($_POST['ids'])) {
			echo 0;
			return ;
		}

		$deleted = model('AreaPopular')->where(array('popu_id'=>array('in',$_POST['ids'])))->delete();
		echo $deleted ? '1' : '0';

	}

	public function getJson(){
		$searchtext = $_REQUEST['searchtext'];
		$city_id = $_REQUEST['city_id'];
		if(!empty($city_id)){$map['city_id'] = $city_id;}
		
		if(!empty($searchtext)){
			$map['title']=array('like',"%".$searchtext."%");
		}
		$populars = model('AreaPopular')->where($map)->limit(20)->field('popu_id as id,title as title')->findAll();
		//echo model('AreaPopular')->getLastSql();
		//dump($populars);
		exit(json_encode($populars));
	}

	public function addStore(){
		$popu_id = $_REQUEST['popu_id'];
		if(empty($popu_id)) {
			$this->error('请选择地点！');
		}
		
		$popular = model('AreaPopular')->where(array('popu_id'=>$popu_id))->find();
		if(empty($popular)) {
			$this->error('请选择地点！');
		}
		$partnerDao = M('Partner');
		
		//查询已添加过的
		$_pids = $partnerDao->where(array('area_id'=>$popular['area_id'],'tag'=>array('like',"%".$popular['title']."%")))
						->field('id')->findAll();
		foreach($_pids as $vo1){
			$pids[]=$vo1['id'];
		}
		
		$where['parent_id'] = 	array('NEQ',0);
		if(!empty($pids))$where['id'] 		= 	array('not in',$pids);
		$where['area_id'] 	= 	$popular['area_id'];
		$where['enable']	=	'Y';
		$where['is_delete']	=	'N';
		$order=' ctime desc ';
		
		$bsubpage = $partnerDao->kPageQuery($where,$order,30);
		//echo $partnerDao->getLastSql();
		//dump($bsubpage);
		
		$this->assign('popular',$popular);
		$this->assign('bsubpage',$bsubpage);
		//查询未与热点地区的店铺
		$this->display();
	}
	
	public function doAddStore(){
		$ids	= explode(',', t($_REQUEST['ids']));
		$popu_id = $_REQUEST['popu_id'];
		if (empty($ids)) {
			echo 0;
			return ;
		}
		if(empty($popu_id)) {
			echo 0;
			return ;
		}
		$popular = model('AreaPopular')->where(array('popu_id'=>$popu_id))->find();
		if(empty($popular)) {
			echo 0;
			return ;
		}
		//$this->_del($_POST['ids'],$popular['title']);
		$partnerDao = M('Partner');
		$data = $partnerDao->where(array('id'=>array('in',$ids)))->findAll();

		foreach($data as $vo){
			if($vo['tag']==null){$vo['tag']='';}
			$tmp_tag = str_replace($popular['title'].",","",$vo['tag']);
			$tmp_tag = str_replace($popular['title'],"",$tmp_tag);
			$tmp_tag = $popular['title'].",".$tmp_tag;
			$tmp_tag = str_replace(",,,",",",$tmp_tag);
			$tmp_tag = str_replace(",,",",",$tmp_tag);
			//echo "<br>id=".$vo['id']."  tag=".$tmp_tag;
			$partnerDao->where(array('id'=>$vo['id']))->save(array('tag'=>$tmp_tag));
		}
		/** 更新店铺总数 **/
		$this->_refreshItemSum($popular['title']);
		
		//$map['id']	= array('in', $_POST['ids']);
		echo '1';
	}
	
	
	public function showStore(){
		$popu_id = $_REQUEST['popu_id'];
		if(empty($popu_id)) {
			$this->error('请选择地点！');
		}
		
		$popular = model('AreaPopular')->where(array('popu_id'=>$popu_id))->find();
		if(empty($popular)) {
			$this->error('请选择地点！');
		}
		$partnerDao = M('Partner');
		
		//查询已添加过的
		$where['parent_id'] = 	array('NEQ',0);
		//$where['id'] 		= 	array('not in',$pids);
		$where['area_id'] 	= 	$popular['area_id'];
		$where['tag'] 	= 	array('like',"%".$popular['title']."%");
		$where['enable']	=	'Y';
		$where['is_delete']	=	'N';
		$order=' ctime desc ';
		
		$bsubpage = $partnerDao->kPageQuery($where,$order,30);
		//echo $partnerDao->getLastSql();
		//dump($bsubpage);
		
		$this->assign('popular',$popular);
		$this->assign('bsubpage',$bsubpage);
		//查询未与热点地区的店铺
		$this->display();
	}
	
	/**
	 * 删除店铺热门地点
	 */
	function delStore(){
		$ids	= explode(',', t($_REQUEST['ids']));
		$popu_id = $_REQUEST['popu_id'];
		if (empty($ids)) {
			echo 0;
			return ;
		}
		if(empty($popu_id)) {
			echo 0;
			return ;
		}
		
		$popular = model('AreaPopular')->where(array('popu_id'=>$popu_id))->find();
		if(empty($popular)) {
			echo 0;
			return ;
		}
		$partnerDao = M('Partner');

		$data = $partnerDao->where(array('id'=>array('in',$ids)))->findAll();
		//echo $partnerDao->getLastSql()."  --  ".$popular['title'];
	
		foreach($data as $vo){
			$tmp_tag = $vo['tag'];
			$tmp_tag = str_replace($popular['title'].",","",$tmp_tag);
			$tmp_tag = str_replace($popular['title'],"",$tmp_tag);
			$tmp_tag = str_replace(",,,",",",$tmp_tag);
			$tmp_tag = str_replace(",,",",",$tmp_tag);
			//echo "<br>id=".$vo['id']."  tag=".$tmp_tag;
			$partnerDao->where(array('id'=>$vo['id']))->save(array('tag'=>$tmp_tag));
		}
		/** 更新店铺总数 **/
		$this->_refreshItemSum($popular['title']);
		
		echo 1;
		return ;
	}
	
	/** 更新店铺总数 **/
	private function _refreshItemSum($tag){
		$map['title'] = $tag;
		$popular = model('AreaPopular')->where($map)->find();
		
		
		$partnerDao = M('Partner');
		$where['tag'] = array('like',"%".$tag.",%");
		$sum = $partnerDao->where($where)->count();
		
		$popular['item_sum']=$sum;
		model('AreaPopular')->save($popular);
	}
}


?>